﻿using Cofe.Core.Interfaces;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Filter
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class CofeFilterFactory : IFilterFactory
    {
        #region Constructor
        
        #endregion

        #region Methods

        private Dictionary<string, Type> getSupportTypes()
        {
            return new Dictionary<string, Type>()
            {
                { "guid", typeof(GuidEntryFilter) },
                { "parentguid", typeof(ParentGuidEntryFilter) },
                { "name", typeof(FileNameEntryFilter) },
                { "filetype", typeof(FileTypeEntryFilter) },
                { "root", typeof(RootEntryFilter) },
                { "page", typeof(PaginateOptionFilter) },
                { "sort", typeof(SortResultOptionFilter) },
                { "subdir", typeof(SearchOptionOptionFilter) },
                { "source", typeof(SearchSourceOptionFilter) }
            };
        }

        public T Construct<T>(string name, string value)
            where T : IFilter
        {
            if (typeof(T) == typeof(IEntryFilter))
            switch (name)
            {
                case "filename" :
                case "name": return (T)(object)new FileNameEntryFilter(value);
                case "fullpath": return (T)(object)new ParseNameEntryFilter(value);
                case "filetype" : return (T)(object)new FileTypeEntryFilter(value);
                case "root": return (T)(object)new RootEntryFilter(PathParsers, value);                
            }

            if (typeof(T) == typeof(IOptionFilter))
                switch (name)
                {                    
                    case "page": return (T)(object)new PaginateOptionFilter(value);
                    case "sort": return (T)(object)new SortResultOptionFilter(PropertyDictionary, value);
                    case "subdir": return (T)(object)new SearchOptionOptionFilter(value);
                    case "source": return (T)(object)new SearchSourceOptionFilter(value);
                }

            return default(T);
        }
        
        #endregion

        #region Data

        private IPathParser[] _pathParsers = null;
        private IPropertyDictionary _propertyDictionary = null;
        
        #endregion

        #region Public Properties

        public Dictionary<string, Type> SupportedFilterTypes
        {
            get { return getSupportTypes(); }
        }

        public IPathParser[] PathParsers
        {
            get { return _pathParsers ?? CofeServices.ServiceLocater.FindAllServices<IPathParser>().ToArray(); }
            set { _pathParsers = value; }
        }
        public IPropertyDictionary PropertyDictionary
        {
            get { return _propertyDictionary ?? CofeServices.ServiceLocater.FindService<IPropertyDictionary>(); }
            set { _propertyDictionary = value; }
        }
        #endregion



       
    
    }
}
